package Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");

        if (username != null) {
            // 用户已登录，检查是否超时
            long currentTime = System.currentTimeMillis();
            long lastAccessTime = session.getLastAccessedTime();
            long elapsedTime = currentTime - lastAccessTime;

            if (elapsedTime < 3600 * 1000) {
                // 用户在1小时内登录过
                out.println("欢迎 " + username + " 访问本网站首页！");
            } else {
                // 用户登录超时
                out.println("登录超时，请重新登录。<a href='login.html'>登录</a>");
            }
        } else {
            // 用户未登录
            out.println("您尚未登录，请先<a href='login.html'>登录</a>");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}